package com.breeze.blockchain.controller.admin;

import com.breeze.blockchain.auth.AuthContextHolder;
import com.breeze.blockchain.controller.admin.form.role.RolePermissionAssignForm;
import com.breeze.blockchain.controller.admin.form.role.RolePermissionPageForm;
import com.breeze.blockchain.controller.admin.vo.PermissionResourceVO;
import com.breeze.blockchain.mybatis.entity.RolePermissionDo;
import com.breeze.blockchain.service.RolePermissionService;
import com.breeze.components.web.core.ResponseDTO;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色权限关联表 控制层。
 *
 * @author breeze
 * @since 2024-05-11 23:21:51
 */
@RestController
@Tag(name = "角色权限关联表接口")
@RequestMapping("api/admin/rolePermission")
public class RolePermissionController {

  @Resource private RolePermissionService rolePermissionService;

  @PostMapping("/assign")
  @Operation(summary = "分配权限")
  public ResponseDTO<Void> assignPermission(@RequestBody RolePermissionAssignForm form) {
    rolePermissionService.assignPermission(form.getRoleId(), form.getPermissionIds());
    return ResponseDTO.ok();
  }

  @PostMapping("/getUserPermissionResource")
  @Operation(summary = "登陆后查询用户所有菜单权限")
  public ResponseDTO<PermissionResourceVO> getUserPermissionResource() {
    return ResponseDTO.ok(
        rolePermissionService.getUserPermissionResource(AuthContextHolder.getAdminLoginUserId()));
  }

  /**
   * 添加角色权限关联表。
   *
   * @param rolePermissionDo 角色权限关联表
   */
  @PostMapping("save")
  @Operation(summary = "保存角色权限关联表")
  public ResponseDTO<Void> save(
      @RequestBody @Parameter(name = "角色权限关联表") RolePermissionDo rolePermissionDo) {
    rolePermissionService.save(rolePermissionDo);
    return ResponseDTO.ok();
  }

  /**
   * 根据主键删除角色权限关联表。
   *
   * @param id 主键
   */
  @GetMapping("remove")
  @Operation(summary = "根据主键角色权限关联表")
  public ResponseDTO<Void> remove(@RequestParam @Parameter(name = "角色权限关联表主键") Long id) {
    rolePermissionService.removeById(id);
    return ResponseDTO.ok();
  }

  /**
   * 根据主键更新角色权限关联表。
   *
   * @param rolePermissionDo 角色权限关联表
   */
  @PostMapping("update")
  @Operation(summary = "根据主键更新角色权限关联表")
  public ResponseDTO<Void> update(
      @RequestBody @Parameter(name = "角色权限关联表主键") RolePermissionDo rolePermissionDo) {
    rolePermissionService.updateById(rolePermissionDo);
    return ResponseDTO.ok();
  }

  /**
   * 查询所有角色权限关联表。
   *
   * @return 所有数据
   */
  @PostMapping("all")
  @Operation(summary = "查询所有角色权限关联表")
  public ResponseDTO<List<RolePermissionDo>> all(
      @RequestBody @Parameter(name = "分页信息") RolePermissionPageForm form) {
    return ResponseDTO.ok(rolePermissionService.list(form.toQueryWrapper()));
  }

  /**
   * 根据角色权限关联表主键获取详细信息。
   *
   * @param id 角色权限关联表主键
   * @return 角色权限关联表详情
   */
  @GetMapping("detail")
  @Operation(summary = "根据主键获取角色权限关联表")
  public ResponseDTO<RolePermissionDo> detail(@RequestParam Long id) {
    return ResponseDTO.ok(rolePermissionService.getById(id));
  }

  /**
   * 分页查询角色权限关联表。
   *
   * @param form 分页对象
   * @return 分页对象
   */
  @PostMapping("page")
  @Operation(summary = "分页查询角色权限关联表")
  public ResponseDTO<Page<RolePermissionDo>> page(
      @RequestBody @Parameter(name = "分页信息") RolePermissionPageForm form) {
    return ResponseDTO.ok(rolePermissionService.page(form.toPage(), form.toQueryWrapper()));
  }
}
